<template>
    <div>
        <van-cell to="/address/fromshop" v-if="defaddress" :title="'收件人:'+name+tel" center :label="detailaddress"  icon="location-o" />
        <van-contact-card v-else type="add"/>
    </div>
</template>

<script>
import { ref } from 'vue' 
import { fetchaddress } from '@/API/goods.js'
import { onBeforeMount } from '@vue/runtime-core'
import { useStore } from 'vuex'
export default {
   setup() {
    const tel = ref('13000000000');
    const name = ref('张三');
    const detailaddress = ref('')
    const store = useStore();
    const onEdit = () => console.log("edit");;
    const defaddress = ref(false)
    const address =ref({})
    const addresslocal = JSON.parse( localStorage.getItem('addresslocal') || '{}' )
    const editaddress = (obj)=>{
        console.log("asdasd");
            name.value = obj.name
            tel.value = obj.tel
            detailaddress.value = obj.province+obj.city+obj.country+obj.addressDetail+obj.postalCode
            defaddress.value=true
    }
    if(   !Object.keys(addresslocal).length  !=0 ){
        // 本地没有地址,发送请求
        onBeforeMount( async ()=>{
        let result =  await fetchaddress(store.state.userInfo.id)
            if(result.length == 0){
                return
            } 
        address.value = result.filter( item => item.isDefault ==1)[0]
        editaddress(address.value)
        localStorage.setItem('addresslocal',JSON.stringify( address.value))  
    })
    }else{
        editaddress(addresslocal)
    }
    
    return {
      tel,
      name,
      defaddress,
      onEdit,
      address,
      detailaddress
    };
  },
}
</script>

<style lang="scss" scoped>

</style>